/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.campusinfocompany.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.campusinfo.modules.campusinfocompany.entity.CampusinfoCompany;
import com.lczyfz.campusinfo.modules.campusinfocompany.service.CampusinfoCompanyService;
import com.lczyfz.campusinfo.modules.sys.entity.User;
import com.lczyfz.campusinfo.modules.sys.service.SystemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.campusinfocompany.entity.LinkCompany;
import com.lczyfz.campusinfo.modules.campusinfocompany.service.LinkCompanyService;

import java.util.List;

/**
 * 公司权限Controller
 * @author zms
 * @version 2018-04-18
 */
@Controller
@RequestMapping(value = "${adminPath}/campusinfocompany/linkCompany")
public class LinkCompanyController extends BaseController {

	@Autowired
	private SystemService systemService;
	@Autowired
	private LinkCompanyService linkCompanyService;
	@Autowired
	private CampusinfoCompanyService campusinfoCompanyService;
	@ModelAttribute
	public LinkCompany get(@RequestParam(required=false) String id) {
		LinkCompany entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = linkCompanyService.get(id);
		}
		if (entity == null){
			entity = new LinkCompany();
		}
		return entity;
	}
	
	@RequiresPermissions("campusinfocompany:linkCompany:view")
	@RequestMapping(value = {"list", ""})
	public String list(LinkCompany linkCompany, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<LinkCompany> page = linkCompanyService.findPage(new Page<LinkCompany>(request, response), linkCompany);
		List<LinkCompany> campusinfoCompanies = linkCompanyService.findList(linkCompany);
		model.addAttribute("campusinfoCompanies", campusinfoCompanies);
		model.addAttribute("page", page);
		return "modules/campusinfocompany/linkCompanyList";
	}

	@RequiresPermissions("campusinfocompany:linkCompany:view")
	@RequestMapping(value = "form")
	public String form(LinkCompany linkCompany, Model model) {
		CampusinfoCompany campusinfoCompany = new CampusinfoCompany();
		List<CampusinfoCompany> campusinfoCompanies = campusinfoCompanyService.findList(campusinfoCompany);
		model.addAttribute("campusinfoCompanies", campusinfoCompanies);
		User campusinfoUser = new User();
		List<User> campusinfoUseres = systemService.findUser(campusinfoUser);
		model.addAttribute("campusinfoUseres", campusinfoUseres);
		//model.addAttribute("linkCompany", linkCompany);
		return "modules/campusinfocompany/linkCompanyForm";
	}

	@RequiresPermissions("campusinfocompany:linkCompany:edit")
	@RequestMapping(value = "save")
	public String save(LinkCompany linkCompany, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, linkCompany)){
			return form(linkCompany, model);
		}
		LinkCompany findlinkCompany = new LinkCompany();
		findlinkCompany.setUser(linkCompany.getUser());
		List<LinkCompany> linkCompanies =linkCompanyService.findList(findlinkCompany);
		if(linkCompanies.isEmpty()) {
			linkCompanyService.save(linkCompany);
			addMessage(redirectAttributes, "添加权限成功");
		} else if(!StringUtils.isBlank(linkCompany.getId())){
			linkCompanyService.save(linkCompany);
			addMessage(redirectAttributes, "修改权限成功");
		} else {
			addMessage(redirectAttributes, "用户已经有权限了");
		}
		return "redirect:"+Global.getAdminPath()+"/campusinfocompany/linkCompany/?repage";
	}
	
	@RequiresPermissions("campusinfocompany:linkCompany:edit")
	@RequestMapping(value = "delete")
	public String delete(LinkCompany linkCompany, RedirectAttributes redirectAttributes) {
		linkCompanyService.delete(linkCompany);
		addMessage(redirectAttributes, "删除用户的归属那家公司成功");
		return "redirect:"+Global.getAdminPath()+"/campusinfocompany/linkCompany/?repage";
	}
	@RequiresPermissions("campusinfocompany:linkCompany:edit")
	@RequestMapping(value = "add")
	public String add(LinkCompany linkCompany, RedirectAttributes redirectAttributes, Model model) {
		User user = systemService.getUser(linkCompany.getUser().getId());
		LinkCompany userlinkCompany = new LinkCompany();
		userlinkCompany.setUser(user);
		CampusinfoCompany campusinfoCompany = new CampusinfoCompany();
		List<CampusinfoCompany> campusinfoCompanies = campusinfoCompanyService.findList(campusinfoCompany);
		model.addAttribute("campusinfoCompanies", campusinfoCompanies);
		model.addAttribute("userlinkCompany", userlinkCompany);
		return "modules/campusinfocompany/linkUserCompany";
	}

}